package com.compty.paint.web.front;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.compty.core.framework.util.StringUtil;
import com.compty.core.page.Page;
import com.compty.core.struts2.BaseAction;
import com.compty.paint.domain.Auction;
import com.compty.paint.domain.AuditState;
import com.compty.paint.domain.ImageInfo;
import com.compty.paint.domain.UploadState;
import com.compty.paint.manager.AuctionManager;
import com.compty.paint.manager.AuditStateManager;
import com.compty.paint.manager.UploadStateManager;
import com.compty.paint.util.Constants;

/**
 * @Todo:拍卖预展Action
 * @CreateTime:2011-8-18 上午10:00:36
 */
public class AuctionAction extends BaseAction<Auction>{
	
	private static final long serialVersionUID = -5216779289136564638L;
	
	private AuctionManager auctionManager;
	private UploadStateManager uploadStateManager;
	private AuditStateManager auditStateManager;
	
	private Auction auction;
	
	public AuctionAction() {
		super();
		super.pageSize = 12;
	}
	
	/**
	 *@Todo:列表显示
	 *@CreateTime:2011-8-18 上午10:23:28
	 * @throws Exception
	 */
	public String list() throws Exception {
		
        Criteria criteria;

        if (sort != null) {
            boolean isAsc = dir.equalsIgnoreCase("asc");
            criteria = auctionManager.createCriteria(sort, isAsc);
        } else {
            criteria = auctionManager.createCriteria();
        }
        
        criteria.addOrder(Order.desc("id"));
        
        
        UploadState  uploadState = uploadStateManager.findUniqueBy("english_name", Constants.Upload_State_Init); //上传状态正常
        criteria = criteria.add(Restrictions.eq("uploadState", uploadState)); 
        AuditState auditState = auditStateManager.findUniqueBy("english_name", Constants.Audit_State_Pass);  //审核状态通过
        criteria = criteria.add(Restrictions.eq("auditState", auditState)); 

        this.page = auctionManager.pagedQuery(criteria, curPage, pageSize);
        
        return "list";
	}
	
	/**
	 *@Todo:显示拍卖预展详情
	 *@CreateTime:2011-8-18 上午10:47:08
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String showDetail() throws Exception {
		
			String idStr = request.getParameter("id");
			if( !StringUtil.isDigit(idStr) ){
				return ERROR;
			}
			Long id = Long.parseLong(idStr);
			this.auction = auctionManager.get(id);
			
			Page page = auctionManager.findImageInfosByAuctionId(1, 10000, id);
			List<ImageInfo>  imageInfos = (List<ImageInfo>) page.getResult();
			if(auction.getImageInfo() != null){
				imageInfos.add(0, auction.getImageInfo());
			}
			
			List<Map> returnList = new ArrayList<Map>();
			for (ImageInfo imageInfo : imageInfos) {
				
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("id", imageInfo.getId());
				map.put("originalName", imageInfo.getOriginalName());
				map.put("sharpId", imageInfo.getSharpId());
				map.put("suffix", imageInfo.getSuffix());
				map.put("memo", imageInfo.getMemo());
				
				returnList.add(map);
			}
			
			String  ajaxData = JSONArray.fromObject(returnList).toString();
			request.setAttribute("ajaxData", ajaxData);
	
		return "showDetail";
	}

	public void setAuctionManager(AuctionManager auctionManager) {
		this.auctionManager = auctionManager;
	}

	public void setUploadStateManager(UploadStateManager uploadStateManager) {
		this.uploadStateManager = uploadStateManager;
	}

	public void setAuditStateManager(AuditStateManager auditStateManager) {
		this.auditStateManager = auditStateManager;
	}

	public Auction getAuction() {
		return auction;
	}

	public void setAuction(Auction auction) {
		this.auction = auction;
	}
	
}
